package world.hezi.bot;

import net.mamoe.mirai.auth.BotAuthorization;
import net.mamoe.mirai.contact.Contact;
import org.apache.ibatis.session.SqlSession;
import world.hezi.handlers.*;
import world.hezi.http.GetBeatsaverLeast;
import net.mamoe.mirai.Bot;
import net.mamoe.mirai.BotFactory;
import net.mamoe.mirai.utils.BotConfiguration;
import world.hezi.mapper.PicInfoMapper;
import world.hezi.mapper.PicTagMapper;
import world.hezi.mapper.UserXpMapper;
import world.hezi.pools.JedisPools;
import world.hezi.pools.MybatisPools;
import world.hezi.scheduler.DiaoScheduler;

import java.io.*;
import java.util.*;

public class BeatsaverBot {
    public static final long QQ_NUMBER;
    public static final String CACHE;
    public static final String PASSWORD;
//    public static final String LOLI_PIC_PATH;
    public static final String PIC_PATH;
    public static final Set<Long> GROUP_NUMBER;
    public static final Set<Long> H_PICTURES;
//    public static String[] NORMAL_LIST;
//    public static String[] LOLI_LIST;
    public static final String CONFIG_PATH;
    public static Map<Long, Set<String>> CONCERN;
    public static Set<String> CHINA;

    static{
        ResourceBundle rb = ResourceBundle.getBundle("bot");
        QQ_NUMBER = Long.parseLong(rb.getString("qqNumber"));
//        LOLI_PIC_PATH = rb.getString("loliPicPath");
        PIC_PATH = rb.getString("picPath");
//        NORMAL_LIST = new File(PIC_PATH).list();
//        LOLI_LIST = new File(LOLI_PIC_PATH).list();
        GROUP_NUMBER = new HashSet<>();
        for (String groupNumber : rb.getString("groupNumber").split(",")) {
            GROUP_NUMBER.add(Long.parseLong(groupNumber));
        }
        H_PICTURES = new HashSet<>();
        for (String groupNumber : rb.getString("hPictures").split(",")) {
            H_PICTURES.add(Long.parseLong(groupNumber));
        }
        PASSWORD = rb.getString("password");
        CONFIG_PATH = rb.getString("configPath");
        CACHE = rb.getString("cache");
        File file = new File(CONFIG_PATH+"\\concern.ser");
        File fileChina = new File(CONFIG_PATH+"\\china.ser");
        if (!file.exists()){
            CONCERN = new HashMap<>();
        }else {
            try {
                FileInputStream fileInputStream = new FileInputStream(file);
                ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
                try {
                    CONCERN = (Map<Long, Set<String>>) objectInputStream.readObject();
                } catch (ClassNotFoundException e) {
                    CHINA = new HashSet<>();
                    e.printStackTrace();
                }
            } catch (IOException e) {
                CONCERN = new HashMap<>();
                e.printStackTrace();
            }

        }
        if (!fileChina.exists()){
            CHINA = new HashSet<>();
        }else {
            try {
                FileInputStream fileInputStream = new FileInputStream(fileChina);
                ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
                try {
                    CHINA = (Set<String>) objectInputStream.readObject();
                } catch (ClassNotFoundException e) {
                    CHINA = new HashSet<>();
                    e.printStackTrace();
                }
            } catch (IOException e) {
                CHINA = new HashSet<>();
                e.printStackTrace();
            }

        }
    }
    public static void main(String[] args) {
//        Bot bot = BotFactory.INSTANCE.newBot(QQ_NUMBER, PASSWORD,new BotConfiguration() {{
//            setHeartbeatStrategy(HeartbeatStrategy.STAT_HB);
//            setWorkingDir(new File(CACHE));
//            setProtocol(MiraiProtocol.ANDROID_WATCH);
//            enableContactCache();
//            fileBasedDeviceInfo();
//        }});
        Bot bot = BotFactory.INSTANCE.newBot(QQ_NUMBER,BotAuthorization.byQRCode(),new BotConfiguration() {{
            setHeartbeatStrategy(HeartbeatStrategy.STAT_HB);
            setWorkingDir(new File(CACHE));
            setProtocol(MiraiProtocol.ANDROID_WATCH);
            enableContactCache();
            fileBasedDeviceInfo();
        }});
        bot.login();
        SqlSession sqlSession = MybatisPools.getSqlSession();
//        bot.getEventChannel().registerListenerHost(new GroupEventHandlers());
        bot.getEventChannel().registerListenerHost(new HelpEventHandlers());
        bot.getEventChannel().registerListenerHost(new RecallEventHandlers());
        bot.getEventChannel().registerListenerHost(new MinecraftServerHandlers());
        bot.getEventChannel().registerListenerHost(new FxxkLongHandlers());
        bot.getEventChannel().registerListenerHost(new DiaoPicHandlers());
        bot.getEventChannel().registerListenerHost(new LinuxCommandHandlers());
//        bot.getEventChannel().registerListenerHost(new AiPicturesHandlers());
//        bot.getEventChannel().registerListenerHost(new ChatHandlers());
        bot.getEventChannel().registerListenerHost(new HPicturesHandlers(
                sqlSession.getMapper(PicInfoMapper.class),
                sqlSession.getMapper(PicTagMapper.class),
                sqlSession.getMapper(UserXpMapper.class),
                JedisPools.getJedis()));
        List<Contact> list = new ArrayList<>();
        for (Long aLong : GROUP_NUMBER) {
            list.add(bot.getGroup(aLong));
        }
        GetBeatsaverLeast getBeatsaverLeast = new GetBeatsaverLeast(list,JedisPools.getJedis());
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    System.out.println("开始尝试");
                    getBeatsaverLeast.getLeast();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        },10000L,10000L);
        List<Long> diao = Arrays.asList(861485420L, 247133571L);
        List<Contact> diaoContact = new ArrayList<>();
        for (Long aLong : diao) {
            diaoContact.add(bot.getGroup(aLong));
        }
        DiaoScheduler.schedule(diaoContact);
    }

}
